package com.masterclient.app.businessmodule.usermodule.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.widget.TextView;

import com.eoner.baselib.utils.activity.UtilActivity;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.masterclient.app.MainActivity;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviterPopModel;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginSuccessData;
import com.masterclient.app.businessmodule.usermodule.login.invite.BindInviteActivity;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/7/13 15:35
 * 功能说明：
 */

public class BindPhoneActivity extends FCBusinessActivity {

    @BindView(R.id.tip_text)
    TextView tipView;

    @BindView(R.id.title_text)
    TextView titleView;

    public final static int REQUEST_CODE = 100;
    public final static int BIND_SUCCESS = 101;

    public String phone, code, auth_type, union_id, nick_name, headimgurl;
    InviterPopModel inviter_pop;
    LoginSuccessData login_success_data;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_login_bind_phone;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setLineHidden(true);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {

        auth_type = getBundleString("auth_type");
        union_id = getBundleString("union_id");
        nick_name = getBundleString("nick_name");
        headimgurl = getBundleString("headimgurl");
        inviter_pop = (InviterPopModel) getIntent().getSerializableExtra("inviter_pop");
        login_success_data = (LoginSuccessData) getIntent().getSerializableExtra("login_success_data");
        setCurrentPage(0, "");
    }


    public void setCurrentPage(int index, String phone) {
        if (!TextUtils.isEmpty(phone))
            this.phone = phone;
        if (index == 1) {
            titleView.setText("请输入验证码");
            tipView.setText("已发送至 " + (TextUtils.isEmpty(phone) ? "手机" : phone.substring(0, 3) + " " + phone.substring(3, 7) + " " + phone.substring(7, phone.length())));
        } else {
            titleView.setText("绑定手机");
            tipView.setText("绑定后，可用微信帐号或手机号快捷登录");
        }

        BindPhoneFragment bindPhoneFragment = new BindPhoneFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("index", index);
        bundle.putString("intentSource", intentSource);
        if (index != 0) {
            bundle.putString("auth_type", auth_type);
            bundle.putString("union_id", union_id);
            bundle.putSerializable("inviter_pop", inviter_pop);
            bundle.putSerializable("login_success_data", login_success_data);
        }
        bindPhoneFragment.setArguments(bundle);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.anim_right, R.anim.anim_left);
        transaction.replace(R.id.frame_layout, bindPhoneFragment);
        transaction.commit();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == BindInviteActivity.REQUEST_CODE) {
            if (resultCode == BindInviteActivity.BIND_SUC_CODE) {
                setResult(BIND_SUCCESS);
                finish();
                if (!UtilActivity.isCurActivity("MainActivity")) {
                    startActivitryWithBundle(MainActivity.class, null);
                }
            }
        }
    }
}
